package com.car.advertising.activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.car.advertising.R;
import com.car.advertising.entity.bean.PassWordBean;
import com.car.advertising.listener.SaveEditListener;
import com.car.advertising.view.EditTextDeleteView;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.interfacelistener.OnRecyclerViewItemLongClickListener;
import com.support.widget.layout.adapter.BaseRecyclerViewAdapter;
import com.support.widget.layout.holder.BaseViewHolder;

import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 14:30 2017/4/20 04
 */

public class UpdatePasswordAdapter extends BaseRecyclerViewAdapter<PassWordBean, BaseViewHolder> {

    private final int UPDATE_PASSWORD_INPUT = 0;
    private final int UPDATE_PASSWORD_COMMIT = 1;

    public UpdatePasswordAdapter(Context context, List<PassWordBean> strings) {
        super(context, strings);
    }

    @Override
    protected void bindView(BaseViewHolder holder, int position, PassWordBean model) {
        if (holder instanceof UpPasswordHolder) {
            UpPasswordHolder upPasswordHolder = (UpPasswordHolder) holder;
            if (position == 0 || position == 1) {
                upPasswordHolder.updateView.setVisibility(View.VISIBLE);
            } else {
                upPasswordHolder.updateView.setVisibility(View.GONE);
            }
            upPasswordHolder.updateNavigationTv.setText(model.getNavigation());
            upPasswordHolder.updateContentEt.setHint(model.getHint());
            upPasswordHolder.updateContentEt.addTextChangedListener(new UpdateTextWatcher(upPasswordHolder.updateContentEt));
            //通过设置tag，防止position紊乱
            upPasswordHolder.updateContentEt.setTag(position);
        } else if (holder instanceof PasswordCommitHolder) {
            PasswordCommitHolder commitHolder = (PasswordCommitHolder) holder;
            commitHolder.passwordHintTv.setText(model.getNavigation());
            commitHolder.commitTv.setText(model.getHint());
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == UPDATE_PASSWORD_INPUT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_update_password, parent, false);
            return new UpPasswordHolder(mContext, view, this);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_update_commit, parent, false);
            return new PasswordCommitHolder(mContext, view, this, getClickListener(), getLongClickListener());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 3) {
            return UPDATE_PASSWORD_COMMIT;
        }
        return UPDATE_PASSWORD_INPUT;

    }

    class UpPasswordHolder extends BaseViewHolder {

        public View updateView;
        public TextView updateNavigationTv;
        public EditTextDeleteView updateContentEt;

        public UpPasswordHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            updateView = (View) rootView.findViewById(R.id.update_view);
            updateNavigationTv = (TextView) rootView.findViewById(R.id.update_password_navigation_tv);
            updateContentEt = (EditTextDeleteView) rootView.findViewById(R.id.update_password_content_et);
        }
    }

    class PasswordCommitHolder extends BaseViewHolder implements OnRecyclerViewItemClickListener, OnRecyclerViewItemLongClickListener {

        public OnRecyclerViewItemClickListener mListener;
        public OnRecyclerViewItemLongClickListener mLongClickListener;
        public TextView passwordHintTv;
        public TextView commitTv;

        public PasswordCommitHolder(Context context, View rootView, RecyclerView.Adapter adapter, OnRecyclerViewItemClickListener clickListener, OnRecyclerViewItemLongClickListener longClickListener) {
            super(context, rootView, adapter, clickListener, longClickListener);
            passwordHintTv = (TextView) rootView.findViewById(R.id.update_password_hint_tv);
            commitTv = (TextView) rootView.findViewById(R.id.update_commit_tv);
            this.mListener = clickListener;
            this.mLongClickListener = longClickListener;
            commitTv.setOnClickListener(this);
            commitTv.setOnLongClickListener(this);
        }

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (mListener != null) {
                mListener.onItemClick(parent, view, position);
            }
        }

        @Override
        public void onItemLongClick(ViewGroup parent, View view, int position) {
            if (mLongClickListener != null) {
                mLongClickListener.onItemLongClick(parent, view, position);
            }
        }
    }

    class UpdateTextWatcher implements TextWatcher {

        private EditText editText;

        public UpdateTextWatcher(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //用户输入完毕后，处理输入数据，回调给主界面处理
            SaveEditListener listener = (SaveEditListener) mContext;
            if (s != null) {
                listener.getEditPositionText(Integer.parseInt(editText.getTag().toString()), s.toString());
            }
        }
    }
}
